package com.ruoyi.ade.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.ade.llm.model.Ade;
import com.ruoyi.ade.tool.AdeCompareUtils;
import com.ruoyi.ade.tool.AdeJsonFormatUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ade.mapper.StandardMapper;
import com.ruoyi.ade.domain.Standard;
import com.ruoyi.ade.service.IStandardService;

/**
 * 标准数据集Service业务层处理
 *
 * @author wxh
 * @date 2025-06-02
 */
@Service
public class StandardServiceImpl implements IStandardService {

    @Autowired
    private StandardMapper standardMapper;

    private static boolean compairng = false;
    private static boolean formating = false;

    public boolean fromat() {
        if (formating) {
            return false;
        }
        new Thread(() -> {
            formating = true;
            try {
                List<Standard> standards = standardMapper.selectStandardList(new Standard());
                for (Standard standard : standards) {
                    List<Ade> ades = JSONArray.parseArray(format(standard.getAdes()), Ade.class);
                    //验证标注结果是否正确
                    if (!AdeJsonFormatUtils.isvalidate(ades)) {  //如果标注的结构有问题 则标注为non-checked
                        Standard r = new Standard();
                        r.setId(standard.getId());
                        r.setStatus("0");
                        standardMapper.updateStandard(r);
                        continue;
                    }
                    for (Ade ade : ades) {  //挨个处理标注的结果
                        //对ADE中药物实体进行格式化
                        if (AdeJsonFormatUtils.formatDrugs(ade)) {
                            Standard r = new Standard();
                            r.setId(standard.getId());
                            r.setAdes(JSONArray.toJSONString(ades));
                            standardMapper.updateStandard(r);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            formating = false;
        }).start();
        return true;
    }

    /**
     * 将算法识别结果 与 标注的结果进行比对    获取比对结果
     */
    public boolean startCompare() {
        if (compairng) {
            return false;
        }
        new Thread(() -> {
            compairng = true;
            try {
                compare();
            } catch (Exception e) {
                e.printStackTrace();
            }
            compairng = false;
        }).start();
        return true;
    }

    /**
     * 对所有的结果比对
     */
    private void compare() {
        List<Standard> list = standardMapper.selectStandardList(new Standard());
        for (Standard standard : list) {
            List<Ade> reference = JSONArray.parseArray(format(standard.getAdes()), Ade.class);
            Standard cond = new Standard();
            cond.setId(standard.getId());

            List<Ade> sag_enire_ades = JSONArray.parseArray(format(standard.getAdesEnire()), Ade.class);
            List<Ade> sag_deepseek_ades = JSONArray.parseArray(format(standard.getAdesDeepseek()), Ade.class);
            List<Ade> sag_qwen_ades = JSONArray.parseArray(format(standard.getAdesQwen()), Ade.class);
            List<Ade> sag_gpt_ades = JSONArray.parseArray(format(standard.getAdesGpt()), Ade.class);

            List<Ade> nag_enire_ades = JSONArray.parseArray(format(standard.getNagAdesEnire()), Ade.class);
            List<Ade> nag_deepseek_ades = JSONArray.parseArray(format(standard.getNagAdesDeepseek()), Ade.class);
            List<Ade> nag_qwen_ades = JSONArray.parseArray(format(standard.getNagAdesQwen()), Ade.class);
            List<Ade> nag_gpt_ades = JSONArray.parseArray(format(standard.getNagAdesGpt()), Ade.class);

            List<Ade> rag_enire_ades = JSONArray.parseArray(format(standard.getRagAdesEnire()), Ade.class);
            List<Ade> rag_deepseek_ades = JSONArray.parseArray(format(standard.getRagAdesDeepseek()), Ade.class);
            List<Ade> rag_qwen_ades = JSONArray.parseArray(format(standard.getRagAdesQwen()), Ade.class);
            List<Ade> rag_gpt_ades = JSONArray.parseArray(format(standard.getRagAdesGpt()), Ade.class);


            cond.setAdesEnireRes(AdeCompareUtils.compare(reference, sag_enire_ades));
            cond.setAdesDeepseekRes(AdeCompareUtils.compare(reference, sag_deepseek_ades));
            cond.setAdesQwenRes(AdeCompareUtils.compare(reference, sag_qwen_ades));
            cond.setAdesGptRes(AdeCompareUtils.compare(reference, sag_gpt_ades));

            cond.setNagAdesEnireRes(AdeCompareUtils.compare(reference, nag_enire_ades));
            cond.setNagAdesDeepseekRes(AdeCompareUtils.compare(reference, nag_deepseek_ades));
            cond.setNagAdesQwenRes(AdeCompareUtils.compare(reference, nag_qwen_ades));
            cond.setNagAdesGptRes(AdeCompareUtils.compare(reference, nag_gpt_ades));

            cond.setRagAdesEnireRes(AdeCompareUtils.compare(reference, rag_enire_ades));
            cond.setRagAdesDeepseekRes(AdeCompareUtils.compare(reference, rag_deepseek_ades));
            cond.setRagAdesQwenRes(AdeCompareUtils.compare(reference, rag_qwen_ades));
            cond.setRagAdesGptRes(AdeCompareUtils.compare(reference, rag_gpt_ades));

            standardMapper.updateStandard(cond);
        }
    }

    private String format(String str) {
        if (StringUtils.isEmpty(str)) return "[]";
        return str;
    }

    /**
     * 检索前limit条标准数据集
     * @param limit
     * @return
     */
    public List<Standard> selectTopStandardList(Long limit){
        return standardMapper.selectTopStandardList(limit);
    }

    /**
     * 查询标准数据集
     *
     * @param id 标准数据集主键
     * @return 标准数据集
     */
    @Override
    public Standard selectStandardById(Long id) {
        return standardMapper.selectStandardById(id);
    }

    public List<Standard>  selectWrongStandardList(Standard standard){
        return standardMapper.selectWrongStandardList( standard);
    }

    /**
     * 查询标准数据集列表
     *
     * @param standard 标准数据集
     * @return 标准数据集
     */
    @Override
    public List<Standard> selectStandardList(Standard standard) {
        return standardMapper.selectStandardList(standard);
    }

    @Override
    public Standard selectOneRandomStandard(Standard standard) {
        return standardMapper.selectOneRandomStandard(standard);
    }

    /**
     * 新增标准数据集
     *
     * @param standard 标准数据集
     * @return 结果
     */
    @Override
    public int insertStandard(Standard standard) {
        return standardMapper.insertStandard(standard);
    }

    /**
     * 修改标准数据集
     *
     * @param standard 标准数据集
     * @return 结果
     */
    @Override
    public int updateStandard(Standard standard) {
        return standardMapper.updateStandard(standard);
    }

    /**
     * 批量删除标准数据集
     *
     * @param ids 需要删除的标准数据集主键
     * @return 结果
     */
    @Override
    public int deleteStandardByIds(Long[] ids) {
        return standardMapper.deleteStandardByIds(ids);
    }

    /**
     * 删除标准数据集信息
     *
     * @param id 标准数据集主键
     * @return 结果
     */
    @Override
    public int deleteStandardById(Long id) {
        return standardMapper.deleteStandardById(id);
    }
}
